<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Verbs</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="flashcard.css" rel="stylesheet" type="text/css">
</head>

<?php

include "Verbs/EnglishConjugator.php";

try {
	$db = new PDO('sqlite:LatinVerbs.db3');
	$q = $db->query('SELECT DISTINCT type FROM verbs;');
	$tmp = $q->fetchAll();
	$type = $tmp[array_rand($tmp)]['type'];
//	echo "<H1>$type</H1>\n";
	$q = $db->query("SELECT DISTINCT verb, meaning, type FROM verbs WHERE type='$type';");
	$tmp = $q->fetchAll();
	$vb = $tmp[array_rand($tmp)];
	$verb = $vb['verb'];
	$meaning = $vb['meaning'];
	$isdep = preg_match('/dep$/', $vb['type']);

	$tmp = $meaning;
	$tmp = preg_replace('/[^\w\-\s].*/', '', $tmp, 1);
	preg_match('/(\S+)(\s?.*)/', $tmp, $matches);
	$everb = $matches[1];
	$erest = $matches[2];

	$persons = array('I', 'thou', 'he', 'we', 'ye', 'they', 'Other');
	$person = $persons[array_rand($persons)];

	$words = array();
	if ($person != 'Other') {
		$vb = array();
		$q = $db->query("SELECT voice, mood, tense, $person FROM verbs WHERE verb='$verb' AND $person NOT NULL;");
		foreach( $q->fetchAll() as $row) {
			$voice = $isdep ? 'active' : $row['voice'];
			$word = str_replace('|', '/', $row[$person]);
			$words[] = array($word, lv2ev($everb, $voice, $row['mood'], $row['tense'], $person).$erest);
		}
	} else {
		$q = $db->query("SELECT * FROM infinitive WHERE verb='$verb';");
		
		$tenses = array('present', 'future', 'perfect');

		foreach( $q->fetchAll() as $row) {
			foreach($tenses as $tense) {
				if ($row[$tense]) {
					$words[] = array($row[$tense], lv2evInfi($everb, $row['voice'], $tense).$erest);
				}
			}
		}
		
		$q = $db->query("SELECT * FROM participle WHERE verb='$verb';");
		$tenses = array('present', 'future', 'perfect', 'gerundive');

		foreach( $q->fetchAll() as $row) {
			foreach($tenses as $tense) {
				if ($row[$tense]) {
					$words[] = array($row[$tense], lv2evPart($everb, $tense).$erest);
				}
			}
		}
		
		$q = $db->query("SELECT * FROM gerund WHERE verb='$verb';");
		
		$cases = array('genitive', 'dative', 'accusative', 'ablative');
		foreach( $q->fetchAll() as $row) {
			foreach($cases as $case) {
				if ($row[$case]) {
					$words[] = array($row[$case], lv2evNoun($everb, 'gerund', $case).$erest);
				}
			}
		}
		
		$q = $db->query("SELECT * FROM supine WHERE verb='$verb';");
		$cases = array('accusative', 'ablative');

		foreach( $q->fetchAll() as $row) {
			foreach($cases as $case) {
				if ($row[$case]) {
					$words[] = array($row[$case], lv2evNoun($everb, 'supine', $case).$erest);
				}
			}
		}

	}
	shuffle($words);
	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
<script type="text/javascript">
<?php
print "var words = new Array(".count($words).");\n";
for($i = 0; $i < count($words); $i++) {
	$tmp = implode("', '", $words[$i]);
	$tmp = preg_replace("/\s*<i>(.*?)<\/i>(.*)$/", " $2 <span class=\"wl\">$1</span>", $tmp);
	print "words[$i] = ['$tmp'];\n";
	
}
?>

var shown = 0;

function updateVoc() {
	var i = Math.floor(shown / 2);

	var j = shown % 2;

	if (j == 0) {
		document.getElementById('v3').innerHTML = words[i][1];
		document.getElementById('v1').innerHTML = "";
	} else {
		document.getElementById('v3').innerHTML = words[i][1];
		document.getElementById('v1').innerHTML = words[i][0];
	}
}

function next() {
	shown++;
	if (shown > words.length * 2 - 1) {
		shown = 0;
		alert('All done!');
	}
	updateVoc();
}

function prev() {
	shown--;
	if (shown < 0) {
		shown = words.length * 2 - 1;
		alert('All done!');
	}
	updateVoc();
}

</script>

<body onload="updateVoc()">

<h1 style="text-align:center; color: #60B0C0;"><?php echo "$meaning - $person"; ?></h1>

<div class="center">
<div id="v2" class="v2">&nbsp;</div>
<div id="v3" class="v3"></div>
<div id="v2" class="v2">&nbsp;</div>
<div id="v1" class="v1"></div>


<a href="#" class="button" id="btnprev" onclick="prev()" accesskey="p">&lt;&lt;</a>
<a href="#" class="button" id="button" onclick="location.reload();">new</a>
<a href="#" class="button" id="btnnext" onclick="next()" accesskey="n">&gt;&gt;</a>



</div>
</body>
</html>
